package com.luo.ssm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @author : luocheng
 * @date : Created in 2021/8/24 18:13
 * @description : SpringMVC的配置类
 */
@Configuration
@ComponentScan("com.luo.ssm.controller")
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    //对静态资源放行
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //配置消息转换器
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //字符串消息转换器
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        //设置字符串消息转换器的默认编码格式
        stringHttpMessageConverter.setDefaultCharset(Charset.forName("utf-8"));
        //JSON数据转换器
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        converters.add(stringHttpMessageConverter);
        converters.add(mappingJackson2HttpMessageConverter);
    }

    //配置文件上传解析器
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        //设置解析器的编码和最大上传大小限制
        multipartResolver.setDefaultEncoding("utf-8");
        multipartResolver.setMaxUploadSize(5242880);
        return multipartResolver;
    }


    //配置thymeleaf模板解析器
    @Bean
    public SpringResourceTemplateResolver templateResolver(){
        SpringResourceTemplateResolver springResourceTemplateResolver = new SpringResourceTemplateResolver();
        //设置模板解析器的前缀和后缀
        springResourceTemplateResolver.setPrefix("/WEB-INF/view/");
        springResourceTemplateResolver.setSuffix(".html");
        //设置模板解析器的编码
        springResourceTemplateResolver.setCharacterEncoding("utf-8");
        //设置模板解析器的模板格式
        springResourceTemplateResolver.setTemplateMode(TemplateMode.HTML);
        return springResourceTemplateResolver;
    }

    //配置thymeleaf模板引擎,并注入模板解析器
    @Bean
    public SpringTemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver){
        SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
        //为模板引擎设置模板解析器
        springTemplateEngine.setTemplateResolver(templateResolver);
        return springTemplateEngine;
    }

    //配置thymeleaf视图解析器，并注入模板引擎
    @Bean
    public ViewResolver viewResolver(SpringTemplateEngine templateEngine){
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        //设置视图解析器的模板引擎
        thymeleafViewResolver.setTemplateEngine(templateEngine);
        //设置视图解析器的编码格式
        thymeleafViewResolver.setCharacterEncoding("utf-8");
        return thymeleafViewResolver;
    }
}
